import { SDKDataMod } from "../SDKDataMode";
import { SDKTools } from "../SDKTools";
import { IAd } from "./IAd";

export class KSAD implements IAd{
    public rewardedVideoAd = null;
    
        public adLoadError: boolean = false;
        public adErrorCode: number = 0;
    
        public adErrorReason: string = "";
    
        private adUnitId: string = "";
        public get my() {
            return SDKTools.m_ADClass;
        }
        public func_初始化(opt: {
            adUnitId: string,
        }) {
            let { adUnitId } = opt;
            // adUnitId = "2300017219_02";
            this.adUnitId = adUnitId
            // 创建激励视频广告全局实例，提前初始化
            console.log("adUnitId", adUnitId)
            this.rewardedVideoAd = this.my.createRewardedVideoAd({
                adUnitId: adUnitId,
            });
            this.rewardedVideoAd.onLoad(() => {
                console.log('激励视频 广告加载成功')
            })
            this.rewardedVideoAd.onError((err) => {
                this.adErrorCode = err.errCode;
                this.adLoadError = true;
                this.adErrorReason = err.errMsg + "(" + err.errCode + ")";
                console.error('激励视频 广告显示失败', err);
                let msg = err.errMsg;
                if (err.errCode) {
                    msg = `${msg}(${err.errCode})`
                }
            });
            return this;
        }
    
        private resolve: Function;
    
    
        public async PlayAd(opt: {
            showLoading?: boolean,
            showError?: boolean,
        }): Promise<{
            res: 0 | 1,
            errMsg: string,
        }> {
            let { showLoading, showError } = opt;
            if (this.adLoadError) {
                if (showError) {
                    let errMsg = SDKDataMod.ADError[this.adErrorCode] || `广告加载失败请稍后再试`;
                    let msg = `${errMsg}`;
                    if (this.adErrorCode) {
                        msg = `${errMsg}(${this.adErrorCode})`;
                    }
                    this.my.showModal({ content: msg } as any);
                }
                return {
                    res: 0,
                    errMsg: "广告加载失败",
                };
            }
    
            return new Promise(resolve => {
                this.resolve = resolve;
                if (showLoading) {
                    this.my.showLoading({
                        content: '广告加载中...',
                    });
                }
                //广告显示成功
                this.rewardedVideoAd.offClose(null);
                this.rewardedVideoAd.onClose(this.onAdClose.bind(this));
    
                this.rewardedVideoAd.load().then(() => {
                    this.rewardedVideoAd.show()
                        .catch(() => {
                            this.rewardedVideoAd.load();
                            if (showLoading) {
                                this.my.hideLoading();
                            }
                            return resolve({
                                res: 0,
                                errMsg: "广告未就绪,请稍后再试",
                            });
                        }).then(() => {
                            //广告展示成功
                            if (showLoading) {
                                this.my.hideLoading();
                            }
                        });
                }).catch(() => {
                    console.log("广告加载失败")
                });
    
    
            });
        }
    
        private onAdClose(res: { isEnded: boolean }) {
            // 用户点击了【关闭广告】按钮
            // 小于 2.1.0 的基础库版本，res 是一个 undefined
            if (res && res.isEnded || res === undefined) {
                // 正常播放结束，可以下发游戏奖励
                // this.奖励();
                return this.resolve({
                    res: 1,
                    errMsg: "播放成功",
                });
            }
            else {
                // 播放中途退出，不下发游戏奖励
                return this.resolve({
                    res: 0,
                    errMsg: "播放中途退出，不下发游戏奖励",
                });
            }
        }
}